/**
 * @(#) MedicalReportProxy.as
 *
 * @date		2010. 01. 18.
 * @version		1.0
 * @author		Goo,Sang Hoi
 * @description	 연말정산  - Proxy
 * 				의료비 지급 명세서 출력 
 */
package com.skt.common_hr.yearadjustment.model
{

	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	
	import flash.net.URLVariables;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class MedicalReportProxy extends CommonProxy
	{
		public static const NAME:String = "MedicalReportProxy";
		public var msgStr:String;
		public var sYear:String;
		
		public var medical:URLVariables = new URLVariables();
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function MedicalReportProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_YEA_MEDICINE");
			var param:Object = new Object();						  

			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			sYear = year;
			
			loadData("Z_HR_YEA_MEDICINE", param, result, fault);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			trace( "suss to load Medical Report proxy");
			//Export 정보
			parseExport(event); 
			//의료비지급명세
			parseMedical(event.result.I_MEDICINE);
			facade.sendNotification(YearAdjustmentFacade.LOAD_MEDICAL_REPORT_COMPLETE, this);
		}

		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in Medical Report: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_MEDICAL_REPORT_FAILED, this);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 기부금 명세서  Export 정보
		 * 
		 * @param
		 * @return 기부금 명세서  Export 정보  Value Object
		 */
		private function parseExport(event:ResultEvent):void {
			medical["E_ENAME"] = event.result["E_ENAME"];
			medical["I_PERNR"] = Variables.yearPernr;
			medical["E_YEAR"] = sYear;
			
			// common infra에 맞게 수정 2010.12.09 by Shin, YUNWOO
			//medical["E_BIRDT"] = DateUtil.toFormatString(event.result["E_BIRDT"] as Date, "YYYY.MM.DD");		
			var tmp:Date = DateUtil.stringToDateTime(event.result["E_BIRDT"]);
			medical["E_BIRDT"] = DateUtil.toDefaultString(tmp);


			medical["E_WORKPLACE"] = event.result["E_WORKPLACE"];
			//사업자 등록번호
			var bizNo:String = event.result["E_BIZNO"];
			if(bizNo != null) {
				medical["E_BIZNO"] = bizNo.substr(0, 3) + "-" + bizNo.substr(3, 2) + "-" +  bizNo.substr(5, 5);
			}
			else {
				medical["E_BIZNO"] = "";
			}
			
			//주민번호
			var regNo:String = event.result["E_REGNO"];
			
			if(regNo != null) {
				medical["E_REGNO"] = regNo.substr(0, 6) + "-" + regNo.substr(6, 7);
			}
			else {
				medical["E_REGNO"] = "";
			}
			medical["E_BIZPLACE"] = event.result["E_BIZPLACE"];
			medical["E_ADDRESS"] = event.result["E_ADDRESS"];
			
			medical["E_10"] = NumberUtil.NumberFormat(Number(event.result["E_10"]));
			medical["E_20"] = NumberUtil.NumberFormat(Number(event.result["E_20"]));
			medical["E_21"] = NumberUtil.NumberFormat(Number(event.result["E_21"]));
			medical["E_30"] = NumberUtil.NumberFormat(Number(event.result["E_30"]));
			medical["E_31"] = NumberUtil.NumberFormat(Number(event.result["E_31"]));
			medical["E_40"] = NumberUtil.NumberFormat(Number(event.result["E_40"]));
			medical["E_41"] = NumberUtil.NumberFormat(Number(event.result["E_41"]));
			medical["E_42"] = NumberUtil.NumberFormat(Number(event.result["E_42"]));
			medical["E_50"] = NumberUtil.NumberFormat(Number(event.result["E_50"]));
			
			medical["E_M1"] = NumberUtil.NumberFormat(Number(event.result["E_M1"]));
			medical["E_M2"] = NumberUtil.NumberFormat(Number(event.result["E_M2"]));
			
			if(medical["E_M1"]=="0") medical["E_M1"] = "";
			if(medical["E_M2"]=="0") medical["E_M2"] = "";

			//신청일자
			var sysDt:Array = DateUtil.toDefaultString(new Date()).split("-");
			medical["E_SYS_YEAR"] 	= sysDt[0];
			medical["E_SYS_MONTH"] 	= sysDt[1];
			medical["E_SYS_DAY"] 	= sysDt[2];
			
		}
		
		/**
		 * 기부금 명세서  정보
		 * 
		 * @param
		 * @return 기부금 명세서  Value Object
		 */
		private function parseMedical(arr:ArrayCollection):void
		{
			if(arr!=null) {
				for (var i:int = 0; i<arr.length; ++i ){
					//주민번호
					var regNo:String = arr[i].REGNO;
					medical["REGNO" + i.toString()] = regNo.substr(0, 6) + "-" + regNo.substr(6, 7);
					medical["DEPTY" + i.toString()] = arr[i].DEPTY;
					//사업자 등록번호
					var bizNo:String = arr[i].BIZNO;
					medical["BIZNO" + i.toString()] = bizNo.substr(0, 3) + "-" + bizNo.substr(3, 2) + "-" +  bizNo.substr(5, 5);	
					medical["MEDNO" + i.toString()] = arr[i].MEDNO;	
					medical["COUNT" + i.toString()] = arr[i].COUNT;
					medical["AMOUNT" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].AMOUNT));
					
					//2013 상호 추가
					medical["SUPNM" + i.toString()] = arr[i].SUPNM;
					
					if(medical["DEPTY" + i.toString()] == '○' ) {
						medical["DEPTY" + i.toString()] = "O";
					}
				}
				medical["M_COUNT"] = arr.length.toString();	
			}
			else {
				medical["M_COUNT"] = 0;
			}
		}
		
		
	}
}